package edu.sdjzu.exam.mapper.exam;


import edu.sdjzu.exam.domain.exam.Exam;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExamMapper {

    @Insert("INSERT INTO Exams (title, description, start_time, end_time, duration_minutes, created_by, course_id) " +
            "VALUES (#{title}, #{description}, #{startTime}, #{endTime}, #{durationMinutes}, #{createdBy}, #{courseId})")
    @Options(useGeneratedKeys = true, keyProperty = "examId")
    void insertExam(Exam exam);

    @Select("SELECT * FROM Exams WHERE exam_id = #{examId}")
    Exam selectExamById(Long examId);

    @Update("UPDATE Exams SET title = #{title}, description = #{description}, start_time = #{startTime}, " +
            "end_time = #{endTime}, duration_minutes = #{durationMinutes} WHERE exam_id = #{examId}")
    void updateExam(Exam exam);

    @Delete("DELETE FROM Exams WHERE exam_id = #{examId}")
    void deleteExam(Long examId);

    @Select("SELECT * FROM Exams WHERE created_by = #{createdBy}")
    List<Exam> selectExamsByCreator(Long createdBy);
    
    // 根据课程ID查询考试
    @Select("SELECT * FROM Exams WHERE course_id = #{courseId}")
    List<Exam> selectExamsByCourse(Long courseId);
    
    // 根据课程ID和创建者查询考试
    @Select("SELECT * FROM Exams WHERE course_id = #{courseId} AND created_by = #{createdBy}")
    List<Exam> selectExamsByCourseAndCreator(@Param("courseId") Long courseId, @Param("createdBy") Long createdBy);
}